<template>
  <div class="dialog">
    <!--    分配角色对话框-->
    <el-dialog
      title="分配角色"
      :visible.sync="setRightDialogVisible"
      width="50%"
      :before-close="handleSetRoleClose"
    >
      <el-form v-loading="loading" element-loading-text="拼命加载中......">
        <el-form-item label="员工角色 :">
          <el-select v-model="roleID" placeholder="请分配角色">
            <el-option
              v-for="(item,index) in roleList"
              :key="index"
              :label="item.Text"
              :value="item.Value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="handleSetRoleClose" :disabled="loading">取 消</el-button>
    <el-button type="primary" @click="submitEvent" :disabled="loading">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import StaffManage from '../../../model/staffManage'
import RoleManage from '../../../model/roleManage'

export default {
  name: 'SetRoleDialog',
  props: {
    setRightDialogVisible: Boolean,
    rowInfoId: Number,
    rowInfo:Object
  },
  data () {
    return {
      loading: false,
      roleID: null,
      roleList: []
    }
  },
  created () {
    this.getRoleInfo()
  },
  methods: {
    handleSetRoleClose (payload) {
      this.$emit('handleSetRoleClose',payload)
    },
    //调用获取角色列表的接口
    async getRoleInfo () {
      const res = await RoleManage.searchList()
      this.roleList = res
      this.roleID=this.rowInfo.RoleID===0?null:this.rowInfo.RoleID
    },
    //点击分配角色对话框中的确定按钮，进行真正的角色分配
    async submitEvent () {
      if (this.roleID === null) {
        return this.$message.error('分配角色不可为空')
      } else {
        try {
          this.loading = true
          await StaffManage.bindRole(this.rowInfoId, this.roleID)
          this.loading = false
          this.$message.success('分配角色成功')
          this.handleSetRoleClose('submitButton')
        } catch (err) {
          this.loading = false
        }

      }
    },
  }
}
</script>

